package visualization.renderers
{
	import model.Model;
	
	import mx.controls.Label;
	
	import visualization.IDataPoint;
	import visualization.Visualization;
	
	public class VerticalList extends WeightedList
	{
		
		public function VerticalList()
		{
			super();
		}
		
		override public function renderFull(source:Visualization):void{
			source.resetLabels();
			var space:Number = source.height;
			var headSize:Number;
			var columnWidth:Number = (this.fields.length==0)?source.width:(source.width / this.fields.length);
			var lab:Label;
			var i:Number = 0;
			var j:Number = 0;
			
			// Render the header
			if(this.showHeader){
				headSize = source.getValue(this.headerSize);
				space -= headSize;
				item.resetLabels();
				for each(var descriptor:Object in this.fields){
					lab = source.getLabel();
					lab.text = source.getTitle(descriptor);
					lab.x = i;
					lab.y = 0;
					i += columnWidth;
				}
				j += 16;
			}
			
			// Render the body
			var totalWeight:Number = 0;
			var minWeight:Number = NaN;
			for each(var item:IDataPoint in source.dataPoints){
				var w:Number = source.getValue(this.elementWeight,item) as Number;
				totalWeight += w;
				if (isNaN(minWeight) || (minWeight > w)) minWeight = w;
			}
			var pixPerUnit:Number = (totalWeight==0)?source.height:(source.height/totalWeight);
			if(minWeight*pixPerUnit < 16) pixPerUnit = 16 / minWeight;
			
			for each(item in source.dataPoints){
				var weight:Number = source.getValue(this.elementWeight,item) as Number;
				var itemHeight:Number = (source.getValue(this.elementWeight, item) as Number) * pixPerUnit;
				i = 0;
				for each(var field:Object in this.fields){
					if(field is Model){
						(item as Visualization).x = i +1;
						(item as Visualization).y = j +1;
						(item as Visualization).height = itemHeight -1;
						(item as Visualization).width = columnWidth -1;
						(item as Visualization).renderThubmnail();
					}
					else{
						lab = source.getLabel();
						lab.text = source.getValue(field, item as IDataPoint);
						lab.x = i;
						lab.y = j + (itemHeight/2) - 16;
						source.background.graphics.beginFill( source.getValue(this.elementColor, item) as uint);
						source.background.graphics.drawRect(i+1, j+1, columnWidth-1, itemHeight-1);
					}
					i += columnWidth;
				}
				j += itemHeight;
			}
		}
		
		override public function clone():VisualizationRenderer{
			var vis:VerticalList = new VerticalList();
			this.initClone(vis);
			return vis;
		}
		
	}
}